package com.atjava.webspider.database;

import java.sql.Connection;
import java.sql.DriverManager;


public class ConnFactory {

	private static final ThreadLocal<Connection> session = new ThreadLocal<Connection>();
	
	private ConnFactory(){}

	public static Connection createConnection(DataSource driver) 
	throws Exception {
		Connection c = (Connection) session.get();
		if (c == null) {
			Class.forName(driver.getDriverCls());
			c = DriverManager.getConnection
			(driver.getUrl(), driver.getUsername(), driver.getPasswd());
			session.set(c);
		}
		return c;
	}
	
	public static Connection getConnection(){
		return (Connection) session.get();
	}

	public static void closeConnection()throws Exception{
		Connection c = (Connection) session.get();
		if (c != null) {
			if (!c.isClosed()) {
				c.close();
			}
		}
		session.set(null);
	}
}
